package com.yunhe.sms.filter;

import com.yunhe.sms.entity.system.Permission;
import com.yunhe.sms.entity.system.User;
import com.yunhe.sms.service.LoginService;
import com.yunhe.sms.service.impl.LoginServiceImpl;
import com.yunhe.sms.service.system.PermissionService;
import com.yunhe.sms.service.system.UserService;
import com.yunhe.sms.service.system.impl.PermissionServiceImpl;
import com.yunhe.sms.service.system.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/29 0029.
 */
@WebFilter(filterName = "permFilter",urlPatterns = "/*")
public class PermFilter implements Filter {
    private PermissionService permissionService;
    private UserService userService;
    private LoginService loginService;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        permissionService = new PermissionServiceImpl();
        userService = new UserServiceImpl();
        loginService = new LoginServiceImpl();
    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)servletRequest;
        HttpServletResponse resp = (HttpServletResponse)servletResponse;
        HttpSession session = req.getSession();

        String requestURI = req.getRequestURI();

        if(requestURI.startsWith("/login")||requestURI.startsWith("/registe")||requestURI.startsWith("/kaptcha.jpg")||requestURI.startsWith("/static/")|| requestURI.equals("/favicon.ico")|| requestURI.equals("/voteitem/toupiao")|| requestURI.equals("/dl/img")|| requestURI.equals("/voteitem/updateVotes")){
            filterChain.doFilter(servletRequest,servletResponse);
        }else{

            User user = (User)session.getAttribute("user");
            if(null == user){
                Cookie[] cookies = req.getCookies();
                if(null != cookies){
                    String userName = null;
                    String password = null;
                    for(Cookie c : cookies){
                        if(c.getName().equals("userName")){
                            userName = c.getValue();
                        }
                        if(c.getName().equals("password")){
                            password = c.getValue();
                        }
                    }
                    user = userService.findOne(userName, password);
                }
            }

            if(null == user){
                resp.sendRedirect(req.getContextPath()+"/login");
            }else{
                session.setAttribute("user",user);

                List menuList = (List)session.getAttribute("menuList");
                Map allPermission = (Map)session.getAttribute("allPermission");


                if(null == menuList){
                   menuList = loginService.findPermiMenu(user.getRoleId());
               }

                session.setAttribute("menuList",menuList);


                if(null == allPermission){
                    allPermission = loginService.allPermission(user.getRoleId());
                }

                if(null != allPermission && allPermission.containsKey(requestURI)){
                    Permission p =(Permission) allPermission.get(requestURI);
                    if(p.getType() == 2){
                        List<Permission> buttonsPermi = permissionService.findButtons(p.getId());
                        req.setAttribute("buttonsPermi",buttonsPermi);
                    }
                    filterChain.doFilter(servletRequest,servletResponse);
                }else{
                    resp.sendError(401,"路径错误或权限不足");
                }


            }
        }
    }
}
